package com.itmk.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.classes.entity.Classes;
import com.itmk.classes.service.ClassesService;
import com.itmk.classes.service.impl.ClassesServiceImpl;
import com.itmk.student.entity.Student;
import com.itmk.student.entity.StudentParm;
import com.itmk.student.entity.StudentPointsVo;
import com.itmk.student.entity.StudentVo;
import com.itmk.student.service.StudentService;
import com.itmk.student.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

///student.do的请求，都会被该servlet处理
@WebServlet("/student.do")
public class StudentServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    private StudentService studentService = new StudentServiceImpl();
    private ClassesService classesService = new ClassesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("studentUI")) {
            studentUI(req, resp);
        } else if (method.equals("addUI")) {
            addUI(req, resp);//新增页面
        } else if (method.equals("addSave")) {
            addSave(req, resp);
        }else if(method.equals("list")){
            list(req,resp);
        }else if(method.equals("editUI")){
            editUI(req,resp);
        }else if(method.equals("editSave")){
            editSave(req,resp);
        }else if(method.equals("delete")){
            delete(req,resp);
        }else if(method.equals("pointList")){
            pointList(req,resp);
        }
    }

    private void pointList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String courseName = req.getParameter("courseName");
        //封装参数
        StudentParm parm = new StudentParm();
        parm.setLimit(limit);
        parm.setPage(page);
        parm.setCourseName(courseName);
        //获取学生id
        HttpSession session = req.getSession();
        Student user = (Student) session.getAttribute("user");
        parm.setStuId(user.getStuId());
        //调用service
        List<StudentPointsVo> pointsVos = studentService.pointList(parm);
        //查询总条数
        int count = studentService.pointCount(parm);
        //返回前端数据
        JSONObject object = new JSONObject();
        object.put("code",0);
        object.put("msg","查询成功");
        object.put("data",pointsVos);
        object.put("count",count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的id
        String stuId = req.getParameter("stuId");
        studentService.deleteById(stuId);
        //写回前端
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","删除成功");
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         //获取前端传递过来的参数
        String stuId = req.getParameter("stuId");
        String calssId = req.getParameter("calssId");
        String stuName = req.getParameter("stuName");
        String phone = req.getParameter("phone");
        String idCard = req.getParameter("idCard");
        String sex = req.getParameter("sex");
        String stuNum = req.getParameter("stuNum");
        String password = req.getParameter("password");
        String joinYear = req.getParameter("joinYear");
        //组装到对象
        Student student = new Student();
        student.setCalssId(calssId);
        student.setPhone(phone);
        student.setIdCard(idCard);
        student.setSex(sex);
        student.setStuName(stuName);
        student.setStuNum(stuNum);
        student.setPassword(password);
        student.setJoinYear(joinYear);
        student.setStuId(stuId);
        //调用更新
        studentService.updateById(student);
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","更新成功!");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String stuName = req.getParameter("stuName");
        //封装参数
        StudentParm parm = new StudentParm();
        parm.setLimit(limit);
        parm.setPage(page);
        parm.setStuName(stuName);
        //查询列表
        List<StudentVo> list = studentService.list(parm);
        //查询总条数
        int count = studentService.getCount(parm);
        //返回前端数据
        JSONObject object = new JSONObject();
        object.put("code",0);
        object.put("msg","查询成功");
        object.put("data",list);
        object.put("count",count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }


    /**
     * 新增保存
     *
     * @param req
     * @param resp
     */
    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String calssId = req.getParameter("calssId");
        String stuName = req.getParameter("stuName");
        String phone = req.getParameter("phone");
        String idCard = req.getParameter("idCard");
        String sex = req.getParameter("sex");
        String stuNum = req.getParameter("stuNum");
        String password = req.getParameter("password");
        String joinYear = req.getParameter("joinYear");
        //组装到对象
        Student student = new Student();
        student.setStuId(UUID.randomUUID().toString().replaceAll("-",""));
        student.setCalssId(calssId);
        student.setPhone(phone);
        student.setIdCard(idCard);
        student.setSex(sex);
        student.setStuName(stuName);
        student.setStuNum(stuNum);
        student.setPassword(password);
        student.setJoinYear(joinYear);
        //调用service层
        studentService.add(student);
        //返回前端需要的数据格式
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "新增成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

     /**
     * 编辑页面
     *
     * @param req
     * @param resp
     */
    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询班级列表
        List<Classes> classesList = classesService.getListforStudent();
        req.setAttribute("editClassesList", classesList);
        //查询回显的数据
        String stuId = req.getParameter("stuId");
        Student student = studentService.getById(stuId);
         req.setAttribute("editStudent", student);
        req.getRequestDispatcher(PREX + "/student_edit.jsp").forward(req, resp);
    }
    /**
     * 新增页面
     *
     * @param req
     * @param resp
     */
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Classes> classesList = classesService.getListforStudent();
        req.setAttribute("classesList", classesList);
        req.getRequestDispatcher(PREX + "/student_add.jsp").forward(req, resp);
    }

    /**
     * 返回学生管理页面
     *
     * @param req
     * @param resp
     */
    private void studentUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/student.jsp").forward(req, resp);
    }
}
